#include "ScnsVl53l0x.c.h"

#if defined(SCNS_VL53L0X_ENABLE)&&SCNS_VL53L0X_ENABLE==1

#include "ScnsIic.h"
#include "ScnsMenu.h"
#include "ScnsSmctpExtOscilloscope.h"
#include "ScnsBeep.h"

static struct
{
    uint8 page;
    char name[8];
    ScnsIicEnum iicN;
    gpio_pin_enum xs;
}ptx[SCNS_VL53L0X_MAX]={
    [SCNS_VL53L0X_FRONT]={.page=7,.name="VL-FR",.iicN=SCNS_IIC_CAMERA,.xs=E13},
    [SCNS_VL53L0X_BACK]={.page=6,.name="VL-BK",.iicN=SCNS_IIC_CAMERA,.xs=E12},
};

const ScnsBspVl53l0xConfig scnsBspVl53l0xConfig[SCNS_VL53L0X_MAX]={
    {.device=0X12,.a=2.913e+05f,.b=316.0f,.c=161.5f,.d=100.0f,},
    {.device=0X16,.a=2.913e+05f,.b=316.0f,.c=161.5f,.d=100.0f,},
};

ScnsStatus scnsBspVl53l0xInit(ScnsVl53l0xEnum vl5N)
{
    gpio_init(ptx[vl5N].xs,GPO,1,GPO_PUSH_PULL);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspVl53l0xPostInit(ScnsVl53l0xEnum vl5N)
{
    scnsMenuSetPage(ptx[vl5N].page,ptx[vl5N].name,NULL);
    scnsMenuAddConstStruct(ptx[vl5N].page,scnsVl53l0xResult[vl5N],millimeters,,SCNS_TYPE_UINT16);
    scnsMenuAddConstStruct(ptx[vl5N].page,scnsVl53l0xResult[vl5N],ambientCount,,SCNS_TYPE_UINT16);
    scnsMenuAddConstStruct(ptx[vl5N].page,scnsVl53l0xResult[vl5N],signalCount,,SCNS_TYPE_UINT16);
    return SCNS_STATUS_OK;
}

void scnsBspVl53l0xReInit(ScnsVl53l0xEnum vl5N)
{
    scnsBeepAdd("77");
    hxPrintfVFL(__file__,(int)__LINE__,"reinit\n");
}

void scnsBspVl53l0xResultUpdate(ScnsVl53l0xEnum vl5N)
{
    if(scnsMenuCanPageSend(ptx[vl5N].page))
    {
        scnsSmctpExtOscilloscopeSendFloat(
            SCNS_SMCTP_CHANNEL_VISUAL_SCOPE,
            scnsVl53l0xResult[vl5N].millimeters,
            scnsVl53l0xResult[vl5N].oriMillimeters,
            scnsVl53l0xResult[vl5N].ambientCount,
            scnsVl53l0xResult[vl5N].signalCount,
        );
    }
}

ScnsStatus ScnsBspVl53l0xSetXs(ScnsVl53l0xEnum vl5N,uint8 dat)
{
    gpio_set_level(ptx[vl5N].xs,dat);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspVl53l0xWrite(ScnsVl53l0xEnum vl5N,uint8 device,const uint8*addr,uint8 addrLen,const uint8*data,uint8 dataLen)
{
    return scnsIicWriteTick(ptx[vl5N].iicN,device,addr,addrLen,data,dataLen,4);
}

ScnsStatus scnsBspVl53l0xRead(ScnsVl53l0xEnum vl5N,uint8 device,const uint8*addr,uint8 addrLen,uint8*data,uint8 dataLen)
{
    return scnsIicReadTick(ptx[vl5N].iicN,device,addr,addrLen,data,dataLen,4);
}

#endif
